﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class ChiTietHoaDonDAO
    {
        SqlConnection conn = null;

        public List<ChiTietHoaDonDTO> LayDanhSachChiTietHoaDon()
        {
            List<ChiTietHoaDonDTO> listCTHD = new List<ChiTietHoaDonDTO>();
            SqlDataReader reader = null;
            try
            {
                conn = DataProvider.ConnectDB();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_LayDanhSachChiTietHoaDon";
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    ChiTietHoaDonDTO cthdDTO = new ChiTietHoaDonDTO();
                    cthdDTO.MaHoaDon = reader.GetInt32(0);
                    cthdDTO.MaThietBi = reader.GetInt32(1);
                    cthdDTO.SoLuong = reader.GetInt32(2);
                    cthdDTO.DonGia = reader.GetDouble(3);
                    cthdDTO.ThanhTien = reader.GetDouble(4);

                    listCTHD.Add(cthdDTO);
                }
            }
            catch (Exception ex)
            { String e = ex.ToString(); }
            finally
            { conn.Close(); }
            return listCTHD;
        }

        public string ThemChiTietHoaDon(ChiTietHoaDonDTO cthdDTO)
        {
            string msg = "";
            try
            {
                conn = DataProvider.ConnectDB();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_ThemChiTietHoaDon";
                SqlParameter para;
                para = cmd.Parameters.Add("@MaHoaDon", SqlDbType.Int);
                para.Value = cthdDTO.MaHoaDon;
                para = cmd.Parameters.Add("@MaThietBi", SqlDbType.Int);
                para.Value = cthdDTO.MaThietBi;
                para = cmd.Parameters.Add("@SoLuong", SqlDbType.Int);
                para.Value = cthdDTO.SoLuong;
                para = cmd.Parameters.Add("@DonGia", SqlDbType.Float);
                para.Value = cthdDTO.DonGia;
                para = cmd.Parameters.Add("@ThanhTien", SqlDbType.Float);
                para.Value = cthdDTO.ThanhTien;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (SqlException ex)
            {
                msg = ex.Message;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return msg;
        }

        public string CapNhatChiTietHoaDon(ChiTietHoaDonDTO cthdDTO)
        {
            string msg = "";
            try
            {
                conn = DataProvider.ConnectDB();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_CapNhatChiTietHoaDon";
                SqlParameter para;
                para = cmd.Parameters.Add("@MaHoaDon", SqlDbType.Int);
                para.Value = cthdDTO.MaHoaDon;
                para = cmd.Parameters.Add("@MaThietBi", SqlDbType.Int);
                para.Value = cthdDTO.MaThietBi;
                para = cmd.Parameters.Add("@SoLuong", SqlDbType.Int);
                para.Value = cthdDTO.SoLuong;
                para = cmd.Parameters.Add("@DonGia", SqlDbType.Float);
                para.Value = cthdDTO.DonGia;
                para = cmd.Parameters.Add("@ThanhTien", SqlDbType.Float);
                para.Value = cthdDTO.ThanhTien;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (SqlException ex)
            {
                msg = ex.Message;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return msg;
        }

        public string XoaChiTietHoaDon(int ma)
        {
            string msg = "";
            try
            {
                conn = DataProvider.ConnectDB();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_XoaChiTietHoaDon";
                SqlParameter para;
                para = cmd.Parameters.Add("@MaHoaDon", SqlDbType.Int);
                para.Value = ma;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (SqlException ex)
            {
                msg = ex.Message;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return msg;
        }

        public List<ChiTietHoaDonDTO> LayDanhSachChiTietHoaDonTheoMaHoaDon(int ma)
        {
            List<ChiTietHoaDonDTO> ds = new List<ChiTietHoaDonDTO>();
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "usp_LayDanhSachChiTietHoaDonTheoMaHoaDon";
            cm.Parameters.Add("@MaHoaDon", SqlDbType.Int).Value = ma;
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                ChiTietHoaDonDTO dto = new ChiTietHoaDonDTO();
                dto.MaHoaDon = dr.GetInt32(0);
                dto.MaThietBi = dr.GetInt32(1);
                dto.SoLuong = dr.GetInt32(2);
                dto.DonGia = dr.GetDouble(3);
                dto.ThanhTien = dr.GetDouble(4);
                ds.Add(dto);
            }
            dr.Close();
            conn.Close();
            return ds;
        }

        public void XoaChiTietHoaDonTheoMaHoaDon(int ma)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "usp_XoaChiTietHoaDonTheoMaHoaDon";
            cm.Parameters.Add("@MaHoaDon", SqlDbType.Int).Value = ma;
            cm.ExecuteNonQuery();
            conn.Close();
        }
    }
}
